package com.example.basic.web.service.shiftRecord.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.basic.base.impl.BaseEntityServiceImpl;
import com.example.basic.exception.ServiceException;
import com.example.basic.utils.PinyinUtils;
import com.example.basic.utils.StringUtils;
import com.example.basic.utils.WubiUtils;
import com.example.basic.web.enums.DataCodeEnum;
import com.example.basic.web.enums.EnableTypeEnum;
import com.example.basic.web.enums.ShiftStatusEnum;
import com.example.basic.web.mapper.shiftRecord.ShiftRecordMapper;
import com.example.basic.web.model.pharmStock.PharmStock;
import com.example.basic.web.model.shiftRecord.ShiftRecord;
import com.example.basic.web.model.terminal.TerminalRecord;
import com.example.basic.web.response.ShiftRecordResponse;
import com.example.basic.web.service.common.CommonService;
import com.example.basic.web.service.dataCode.DataCodeService;
import com.example.basic.web.service.pharmStock.PharmStockService;
import com.example.basic.web.service.shiftRecord.ShiftRecordService;
import com.example.basic.web.service.terminal.TerminalRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.ArrayList;
import java.util.List;

import static com.example.basic.utils.CheckUtil.notNull;


@Service
public class ShiftRecordServiceImpl extends BaseEntityServiceImpl<ShiftRecordMapper, ShiftRecord> implements ShiftRecordService {

    @Resource
    private ShiftRecordMapper shiftRecordMapper;

    @Resource
    private DataCodeService dataCodeService;

    @Resource
    private PharmStockService pharmStockService;

    @Resource
    private TerminalRecordService terminalRecordService;

    @Resource
    private CommonService commonService;

    @Resource
    private HttpServletRequest request;

    /**
     * 新增
     * @param shiftRecord 实体
     * @return
     */
    @Override
    public ShiftRecord add(ShiftRecord shiftRecord) {
        notNull(shiftRecord,"实体不能为空");
        notNull(shiftRecord.getRemainsCode(),"遗体编码不能为空");
        notNull(shiftRecord.getShiftPharmCode(),"移位仓库不能为空");
        notNull(shiftRecord.getShiftZoneCode(),"移位库区不能为空");
        notNull(shiftRecord.getShiftDepotCode(),"移位库位不能为空");
        //查询库位是否使用
        PharmStock pharmStock = pharmStockService.judgeStock(shiftRecord.getShiftPharmCode(),shiftRecord.getShiftZoneCode(),shiftRecord.getShiftDepotCode());
        if(pharmStock != null){
            throw new ServiceException("该库位已使用");
        }

        //查询库存信息
        PharmStock judgeRemains = pharmStockService.queryByRemainsCode(shiftRecord.getRemainsCode());
        if(judgeRemains == null || judgeRemains.getOutDatetime() != null ){
            throw new ServiceException("遗体编码无效");
        }

        shiftRecord.setPharmCode(judgeRemains.getPharmCode());
        shiftRecord.setZoneCode(judgeRemains.getZoneCode());
        shiftRecord.setDepotCode(judgeRemains.getDepotCode());
        shiftRecord.setStatus(ShiftStatusEnum.WAIT.getType());

        //移位单编码
        String shiftCode = dataCodeService.generateDataCodeNo(DataCodeEnum.SHIFT_CODE.getType());
        shiftRecord.setShiftCode(shiftCode);

        preCreate(shiftRecord);
        save(shiftRecord);

        return shiftRecord;
    }

    /**
     * 移位审核
     * @param shiftCode 移位单编码
     * @return
     */
    @Override
    public Boolean audit(String shiftCode) {
        notNull(shiftCode,"移位单编码不能为空");

        LambdaQueryWrapper<ShiftRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShiftRecord::getShiftCode,shiftCode);
        ShiftRecord shiftRecord = this.mapper.selectOne(lqw);

        if(shiftRecord == null){
            throw new ServiceException("未获取到需要审核的移位单");
        }

        shiftRecord.setStatus(ShiftStatusEnum.IN_STORAGE.getType());
        preUpdate(shiftRecord);
        this.updateById(shiftRecord);

        //调度系统指令 todo
        this.shiftStruct(shiftRecord);

        return true;
    }

    private void shiftStruct(ShiftRecord shiftRecord) {

        List<TerminalRecord> terminalRecordList = new ArrayList<>();
        //接驳台->运送车
        TerminalRecord terminalRecord = new TerminalRecord();
        terminalRecord.setOperType("遗体出库");
        terminalRecord.setRemainsCode(shiftRecord.getRemainsCode());
        terminalRecord.setInstructTime(commonService.getDataBaseTime());
        terminalRecord.setInstructStart(shiftRecord.getDepotCode());
        terminalRecord.setInstructEnd("转运车");
        terminalRecord.setStatus("1");
        terminalRecord.setOperPerson((String) request.getAttribute("userCode"));
        terminalRecord.setSortNo(1);
        terminalRecord.setDocumentNo(shiftRecord.getShiftCode());
        terminalRecordList.add(terminalRecord);

        //可升降转运车->库位
        terminalRecord = new TerminalRecord();
        terminalRecord.setOperType("遗体入库");
        terminalRecord.setRemainsCode(shiftRecord.getRemainsCode());
        terminalRecord.setInstructStart("转运车");
        terminalRecord.setInstructEnd(shiftRecord.getShiftDepotCode());
        terminalRecord.setStatus("0");
        terminalRecord.setSortNo(2);
        terminalRecord.setDocumentNo(shiftRecord.getShiftCode());
        terminalRecordList.add(terminalRecord);

        terminalRecordService.batchAdd(terminalRecordList);
    }


    /**
     * 取消移位
     * @param shiftCode 移位单编码
     * @return
     */
    @Override
    public Boolean cancel(String shiftCode) {
        notNull(shiftCode,"移位单编码不能为空");

        LambdaQueryWrapper<ShiftRecord> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShiftRecord::getShiftCode,shiftCode);
        ShiftRecord shiftRecord = this.mapper.selectOne(lqw);

        if(shiftRecord == null){
            throw new ServiceException("未获取到需要取消的移位单");
        }

        if(!shiftRecord.getStatus().equals(ShiftStatusEnum.WAIT.getType())){
            throw new ServiceException("移位中，无法取消");
        }
        if(this.mapper.deleteById(shiftRecord.getId()) > 0){
            return true;
        }

        throw new ServiceException("取消失败");
    }

    /**
     * 移位单列表
     * @param queryPage   分页
     * @param searchParam 遗体名称，拼音码，五笔码
     * @param sex         性别
     * @param type 类别
     * @param pharmCode   库房编码
     * @param zoneCode    分区编码
     * @param depotCode   库位编码
     * @param beginTime 开始日期
     * @param endTime 结束日期
     * @return
     */
    @Override
    public IPage<ShiftRecordResponse> queryList(Page<ShiftRecordResponse> queryPage, String searchParam, String sex, String type, String pharmCode, String zoneCode, String depotCode, String beginTime, String endTime) {
        return this.mapper.queryList(queryPage,searchParam,sex,type,pharmCode,zoneCode,depotCode,beginTime,endTime);
    }
}
